package com.jcfk.eam.service.chat;

import cn.hutool.core.date.DateUtil;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.eam.api.SysServiceFeign;
import com.jcfk.eam.domain.dto.base.EamRepairOrderDTO;
import com.jcfk.eam.domain.message.WeChatSendRequest;
import com.jcfk.eam.domain.message.WeChatSendSecretRequest;
import com.jcfk.eam.domain.message.WeChatSendTextCard;
import com.jcfk.eam.enums.MessageTypeWeChatEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.concurrent.CompletableFuture;

@Service
public class EnterpriseWechatService {
    @Value("${app.enterprise-wechat.repair-chatid}")
    private String chatId;
    @Value("${app.h5app-url.repair-url}")
    private String repairUrl;
    @Value("${app.h5app-url.repair-page}")
    private String repairPage;
    @Value("${app.h5app-url.repair-empNo}")
    private String empNo;
    @Autowired
    private SysServiceFeign sysServiceFeign;

    /**
     * 发送企业微信群通知
     *
     * @param eamRepairOrder
     */
    public void sendWeChat(String assetName, EamRepairOrderDTO eamRepairOrder) {
        WeChatSendSecretRequest weChatSendSecretRequest = new WeChatSendSecretRequest();
        weChatSendSecretRequest.setEmpNo(empNo);
        weChatSendSecretRequest.setToPage(repairPage);
        WeChatSendRequest sendRequest = new WeChatSendRequest();
        weChatSendSecretRequest.setWeChatSendRequest(sendRequest);
        WeChatSendTextCard textCard = new WeChatSendTextCard();
        textCard.setTitle(String.format("%s出现故障", assetName));
        textCard.setDescription(String.format("<div class=\"normal\">故障描述：【%s】</div><div class=\"normal\">报修时间：%s</div>", eamRepairOrder.getRemarks(), DateUtil.now()));
        textCard.setUrl(repairUrl);
        sendRequest.setTextcard(textCard);
        sendRequest.setChatid(chatId);
        sendRequest.setMsgtype(MessageTypeWeChatEnum.TEXT_CARD.getValue());
        System.out.println("------------------");
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        try {
            CompletableFuture.runAsync(() -> {
                RequestContextHolder.setRequestAttributes(requestAttributes);
                sysServiceFeign.sendMessageSecretChat(weChatSendSecretRequest);
            });
        } catch (Exception ex) {
            ex.printStackTrace();
            // throw new CustomException(ex.getMessage());
        }
    }
}
